package widgets;

import javax.swing.*;
import java.awt.*;

/**
 * @author Neil Dickson
 */
public class ImageButton extends CustomButton {
	private Image	upImage;
	private Image	hoverImage;
	private Image	downImage;
	private Image	disabledImage;
	public ImageButton(String name) {
		this(name,true);
	}
	public ImageButton(String name, boolean isEnabled) {
		this(new ImageIcon(name + "Up.gif").getImage(),
			 new ImageIcon(name + "Hover.gif").getImage(),
			 new ImageIcon(name + "Down.gif").getImage(),
			 new ImageIcon(name + "Disabled.gif").getImage(),
			 isEnabled);
	}

	public ImageButton(Image image) {
		this(image,image,image,image,true);
	}
	public ImageButton(Image up, Image hover, Image down, Image disabled) {
		this(up,hover,down,disabled,true);
	}
	public ImageButton(Image up, Image hover, Image down, Image disabled, boolean isEnabled) {
		super(isEnabled);
		upImage = up;
		hoverImage = hover;
		downImage = down;
		disabledImage = disabled;
		if (up!=null) {
			int width = up.getWidth(null);
			int height = up.getHeight(null);
			Dimension dim = new Dimension(width, height);
			setSize(dim);
			setMinimumSize(dim);
			setMaximumSize(dim);
			setPreferredSize(dim);
		}
	}

	public void setImages(Image up, Image hover, Image down, Image disabled) {
		upImage = up;
		hoverImage = hover;
		downImage = down;
		disabledImage = disabled;
		repaint();
	}

	protected void paintComponent(Graphics g) {
		if (!isEnabled) {
			g.drawImage(disabledImage, 0, 0, null);
		}
		else if (!isInside) {
			g.drawImage(upImage, 0, 0, null);
		}
		else if (!isPressed) {
			g.drawImage(hoverImage, 0, 0, null);
		}
		else {
			g.drawImage(downImage, 0, 0, null);
		}
	}
}
